package com.cqc.topbarbaseactivitydemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * Created by cui on 2017/4/27.
 */

public abstract class TopBarBaseActivity extends AppCompatActivity {

    private Toolbar toolbar;
    protected Context context;
    private FrameLayout frameLayout;
    private TextView tv_title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_top_bar);
        context = this;
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//不显示title

        tv_title = (TextView) toolbar.findViewById(R.id.tv_title);

        frameLayout = (FrameLayout) findViewById(R.id.frameLayout);

        LayoutInflater.from(context).inflate(getContentView(), frameLayout);
        initViews(savedInstanceState);
    }

    public abstract int getContentView();

    protected abstract void initViews( Bundle savedInstanceState);

    protected void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
    }

    /**
     * 设置左侧图标和点击事件
     */
    public abstract interface OnClickListener {
        void onClick();
    }

    OnClickListener onClickListenerLeftButton;

    protected void setTopBarLeftButton(int resId, OnClickListener onClickListener) {
        toolbar.setNavigationIcon(resId);
        this.onClickListenerLeftButton = onClickListener;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onClickListenerLeftButton.onClick();
        }
        if (item.getItemId() == R.id.item_right) {
            onClickListenerRightButton.onClick();
        }
        return true;
    }

    protected int menuResId;
    protected String menuTitle;
    OnClickListener onClickListenerRightButton;

    protected void setTopBarRightButton(String title, OnClickListener onClickListener) {
        this.menuTitle = title;
        this.onClickListenerRightButton = onClickListener;
    }

    protected void setTopBarRightButton(String title, int menuResId, OnClickListener onClickListener) {
        this.menuTitle = title;
        this.menuResId = menuResId;
        onClickListenerRightButton = onClickListener;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (!TextUtils.isEmpty(menuTitle)) {
            menu.findItem(R.id.item_right).setTitle(menuTitle);
        }
        if (menuResId != 0) {
            menu.findItem(R.id.item_right).setIcon(menuResId);
        }
        return super.onPrepareOptionsMenu(menu);
    }
}
